package com.snts.synchronization.client;

import java.io.InputStreamReader;
import java.net.URL;
import java.sql.SQLException;

import com.snts.synchronization.Utils;
import com.snts.synchronization.core.Change;
import com.snts.synchronization.core.SyncRequest;
import com.snts.synchronization.core.SyncResponse;

public class Test {
	
	public static void main(String[] args) throws Exception 
	{
		SyncClient syncClient = new SyncClient(new URL("http://localhost:8080/Synchronizator"));
		String sessionId = syncClient.auth("60bb4c86-e2cd-11e1-841e-eb4b539b55ff", "123");
		SyncRequest syncRequest = new SyncRequest();
		syncRequest.setSessionId(sessionId);
		ChangesParser changesParser = new ChangesParser(Utils.createSynchronization());
		for(Change change : changesParser.parse(new InputStreamReader(Test.class.getResourceAsStream("Changes.txt"))))
		{
			syncRequest.addChange(change);
		}
		SyncResponse syncResponse = syncClient.sync(syncRequest);
		System.out.println(syncResponse.getStatus());
		if(syncResponse.getErrorObject() != null){
			System.out.println(syncResponse.getErrorObject());
			System.exit(1);
		}
		for(Change change : syncResponse.getChanges()){
			System.out.print(Change.Type.INSERT == change.getType() ? "INSERT" : "UPDATE");
			System.out.print("(table=" + change.getTable().getName() + ", rowId=" + change.getRowId().toString() + ", )" );
		}
	}

}
